﻿using BCCommon.TMS.Order;
using BCData.Common.EvaluationTag;
using BCData.TMS.Order.Order;
using BCData.TMS.Order.OrderEvaluation;
using BCDto.TMS.Order.OrderEvaluation;
using BCEntity.Common.EvaluationTag;
using BCEntity.TMS.Order.Order;
using BCEntity.TMS.Order.OrderEvaluation;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.TMS.Order.OrderEvaluation
{
    public class OrderEvaluationService : IOrderEvaluationService
    {
        private readonly IEvaluationTagData evaluationTagData;
        private readonly IOrderEvaluationData orderEvaluationData;
        private readonly IOrderData orderData;

        public OrderEvaluationService(IEvaluationTagData evaluationTagData, IOrderEvaluationData orderEvaluationData, IOrderData orderData)
        {
            this.evaluationTagData = evaluationTagData;
            this.orderEvaluationData = orderEvaluationData;
            this.orderData = orderData;
        }

        public OrderEvaluationDto Create(long orderId)
        {
            OrderEntity orderEntity = GetOrder(orderId);
            OrderEvaluationDto orderEvaluationDto = GetEntity(orderId);
            if (orderEvaluationDto == null)
            {
                OrderEvaluationEntity orderEvaluationEntity = new OrderEvaluationEntity();
                orderEvaluationEntity.OrderId = orderId;
                orderEvaluationEntity.DriverId = orderEntity.DriverId.Value;
                orderEvaluationEntity.UserId = orderEntity.UserId;
                var result = this.orderEvaluationData.Create(orderEvaluationEntity).GetAwaiter().GetResult();
                return result.As<OrderEvaluationDto>();
            }
            else
            {
                return orderEvaluationDto;
            }
        }

        public OrderEvaluationDto GetEntity(long orderId)
        {
            var result = this.orderEvaluationData.GetEntity(orderId).GetAwaiter().GetResult();
            return result.As<OrderEvaluationDto>();
        }

        public OrderEvaluationDto UserEvaluate(OrderEvaluationDto orderEvaluationDto)
        {
            OrderEntity orderEntity = GetOrder(orderEvaluationDto.OrderId);
            OrderEvaluationDto evaluationDto = this.GetEntity(orderEvaluationDto.OrderId);
            if (orderEntity.OrderStatus != OrderStatus.Complete)
            {
                throw new ArgumentException("订单未完成，无法进行评价");
            }
            if (evaluationDto == null)
            {
                throw new ArgumentException("订单评价不存在");
            }
            if (evaluationDto.UserId != orderEvaluationDto.UserId)
            {
                throw new ArgumentException("请勿评价他人订单");
            }
            if (evaluationDto.IsUserEvaluated)
            {
                throw new ArgumentException("用户已评价完成，无需重复操作");
            }

            if (orderEvaluationDto.UserEvaluationContent.Length > 200)
            {
                throw new ArgumentException("评价内容应在200字内");
            }
            if (orderEvaluationDto.UserEvaluationContent.Length < 20)
            {
                this.evaluationTagData.Create(new EvaluationTagEntity
                {
                    UserId = orderEntity.UserId,
                    TagContent = orderEvaluationDto.UserEvaluationContent
                }).GetAwaiter().GetResult();
            }
            var result = this.orderEvaluationData.UserEvaluate(orderEvaluationDto.As<OrderEvaluationEntity>()).GetAwaiter().GetResult();
            return result.As<OrderEvaluationDto>();
        }

        public OrderEvaluationDto VehicleEvaluate(OrderEvaluationDto orderEvaluationDto)
        {
            OrderEntity orderEntity = GetOrder(orderEvaluationDto.OrderId);
            OrderEvaluationDto evaluationDto = this.GetEntity(orderEvaluationDto.OrderId);
            if (orderEntity.OrderStatus != OrderStatus.Complete)
            {
                throw new ArgumentException("订单未完成，无法进行评价");
            }
            if (evaluationDto == null)
            {
                throw new ArgumentException("订单评价不存在");
            }
            if (evaluationDto.DriverId != orderEvaluationDto.DriverId)
            {
                throw new ArgumentException("请勿评价他人订单");
            }
            if (evaluationDto.IsVehicleEvaluated)
            {
                throw new ArgumentException("司机已评价完成，无需重复操作");
            }

            if (orderEvaluationDto.VehicleEvaluationContent.Length > 200)
            {
                throw new ArgumentException("评价内容应在200字内");
            }
            if (orderEvaluationDto.VehicleEvaluationContent.Length < 20)
            {
                //TODO:司机评价，用户评价，会重复创建标签。
                //this.evaluationTagData.CheckExists()
                this.evaluationTagData.Create(new EvaluationTagEntity()
                {
                    UserId = orderEntity.UserId,
                    TagContent = orderEvaluationDto.VehicleEvaluationContent
                }).GetAwaiter().GetResult();
            }

            var result = this.orderEvaluationData.VehicleEvaluate(orderEvaluationDto.As<OrderEvaluationEntity>()).GetAwaiter().GetResult();
            return result.As<OrderEvaluationDto>();
        }

        private OrderEntity GetOrder(long orderId)
        {
            OrderEntity orderEntity = this.orderData.GetEntity(orderId).GetAwaiter().GetResult();
            return orderEntity;
        }
    }
}
